package cn.net.pap.pinyincut;

import cn.net.pap.pinyincut.core.PinyinCutAnalyzer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;

import java.io.IOException;

public class PinyinCutAnalysisTest  {

    public static void main(String[] args) throws Exception{
        String text = "zhonguo";
        try {
            Analyzer py = new PinyinCutAnalyzer();
            TokenStream ts = py.tokenStream("aa", text);
            PositionIncrementAttribute posIncAtt = ts.getAttribute(PositionIncrementAttribute.class);
            OffsetAttribute offsetAttribute = ts.getAttribute(OffsetAttribute.class);
            CharTermAttribute termAtt = ts.getAttribute(CharTermAttribute.class);
            ts.reset();

            while (ts.incrementToken()) {
                int length = termAtt.length();
                if (length == 0) {
                    throw new IllegalArgumentException("term: " + text + " analyzed to a zero-length token");
                }
                System.out.println("term: " + text + " analyzed to a token (" + termAtt +
                        ") (got: " + posIncAtt.getPositionIncrement() + ")"
                        + "startOffset: " + offsetAttribute.startOffset()
                        + "endOffset: " + offsetAttribute.endOffset()
                );
            }

            while (ts.incrementToken()) {
                char buff[] = termAtt.buffer();
                System.out.println("increment:      " + posIncAtt.getPositionIncrement());
//                System.out.print(new String(buff, 0, ca.length()) + "|");
            }
            ts.end();
            py.close();
            System.out.println();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
